#include <QBrush>
#include "track_data_table_model_02.h"

#include "constant_variable.h"
#include "track_data_02.h"

Track_data_table_model_02::Track_data_table_model_02(QObject *parent)
    : QAbstractTableModel(parent)
    , list_(new QList<Track_data_02*> ())
    , backup_list_(new QList<Track_data_02*>())
{
//    init_connections();


//    qRegisterMetaType<QAbstractItemModel::LayoutChangeHint>("QAbstractItemModel::LayoutChangeHint");
}

Track_data_table_model_02::~Track_data_table_model_02()
{
    if (nullptr != list_) {
        delete list_;
        list_ = nullptr;
    }
}

int Track_data_table_model_02::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    int row_count(0);

    if ( !list_->isEmpty()) {
        row_count = list_->count();
    }

    return row_count;
}

int Track_data_table_model_02::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return 17;
}

QVariant Track_data_table_model_02::data(const QModelIndex &index, int role) const
{
    if ( !index.isValid()) {
        return QVariant();
    }

    QVariant var = QVariant();
    // 获取对应行处数据序列号
    Track_data_02* data = list_->at(index.row());

    // Reset to show track data
//    if ( data->is_show_track()) {
//        emit si_is_show_row(data->is_show_track(), index.row());
//    } else {

//    }



    if (Qt::DisplayRole == role) {
        switch (index.column()) {
        case 0:
            if ( data->get_data()->contains(TRACK_ID)) {
                var = data->get_data()->value(TRACK_ID);
            } else {
                var = NONE;
            }
            break;

        case 1:
            if ( data->get_data()->contains(TIME)) {
                var = data->get_data()->value(TIME);
            } else {
                var = NONE;
            }
            break;

        case 2:
            if ( data->get_data()->contains(CALL_SIGN)) {
                var = data->get_data()->value(CALL_SIGN);
            } else {
                var = NONE;
            }
            break;

        case 3:
            if ( data->get_data()->contains(SSR_CODE)) {
                var = data->get_data()->value(SSR_CODE);
            } else {
                var = NONE;
            }
            break;

        case 4:
            if ( data->get_data()->contains(H0)) {
                var = data->get_data()->value(H0);
            } else if (data->get_data()->contains("h")) {
                var = (data->get_data()->value("h").toString() + ", QNH Applied");
            } else {
                var = NONE;
            }
            break;

        case 5:
            if ( data->get_data()->contains(CFL)) {
                var = data->get_data()->value(CFL);
            } else {
                var = NONE;
            }
            break;

        case 6:
            if ( data->get_data()->contains(LONGITUDE)) {
                var = data->get_data()->value(LONGITUDE);
            } else {
                var = NONE;
            }
            break;

        case 7:
            if ( data->get_data()->contains(LATITUDE)) {
                var = data->get_data()->value(LATITUDE);
            } else {
                var = NONE;
            }
            break;

        case 8:
            if ( data->get_data()->contains(SPEED)) {
                var = data->get_data()->value(SPEED);
            } else {
                var = NONE;
            }
            break;

        case 9:
            if ( data->get_data()->contains(HEADING)) {
                var = data->get_data()->value(HEADING);
            } else {
                var = NONE;
            }
            break;

        case 10:
            if ( data->get_data()->contains(ARR_AIRPORT)) {
                var = data->get_data()->value(ARR_AIRPORT);
            } else {
                var = NONE;
            }
            break;

        case 11:
            if ( data->get_data()->contains(DEP_AIRPORT)) {
                var = data->get_data()->value(DEP_AIRPORT);
            } else {
                var = NONE;
            }
            break;

        case 12:
            if ( data->get_data()->contains(EOBT)) {
                var = data->get_data()->value(EOBT);
            } else {
                var = NONE;
            }
            break;

        case 13:
            if ( data->get_data()->contains(RVSM)) {
                var = data->get_data()->value(RVSM);
            } else {
                var = NONE;
            }
            break;

        case 14:
            if ( data->get_data()->contains(FR)) {
                var = data->get_data()->value(FR);
            } else {
                var = NONE;
            }
            break;

        case 15:
            if ( data->get_data()->contains(ACTF_TURB)) {
                var = data->get_data()->value(ACTF_TURB);
            } else {
                var = NONE;
            }
            break;

        case 16:
            if ( data->get_data()->contains(TIME_STAMP)) {
                var = data->get_data()->value(TIME_STAMP);
            } else {
                var = NONE;
            }
            break;
        }

        return var;
    }

    // Setup background color
    if (Qt::BackgroundRole == role) {
        switch (index.row() % 3) {
        case 0:
            var = QBrush(Qt::yellow, Qt::SolidPattern);
            break;

        case 1:
            var = QBrush(QColor(187, 255, 255), Qt::SolidPattern);
            break;

        case 2:
            var = QBrush(QColor(144, 238, 144), Qt::SolidPattern);
            break;
        }

        return var;
    }

//    emit si_is_show_row(data->is_show_track(), index.row());

    return var;
}

QVariant Track_data_table_model_02::headerData(int section,
                                               Qt::Orientation orientation,
                                               int role) const
{
    if (Qt::DisplayRole == role && Qt::Horizontal == orientation) {
        QString header;

        switch (section) {
        case 0:
            header = TRACK_ID.toUpper();
            break;

        case 1:
            header = TIME.toUpper();
            break;

        case 2:
            header = CALL_SIGN.toUpper();
            break;

        case 3:
            header = SSR_CODE.toUpper();
            break;

        case 4:
            header = H0.toUpper();
            break;

        case 5:
            header = CFL;
            break;

        case 6:
            header = LONGITUDE.toUpper();
            break;

        case 7:
            header = LATITUDE.toUpper();
            break;

        case 8:
            header = SPEED;
            break;

        case 9:
            header = HEADING;
            break;

        case 10:
            header = ARR_AIRPORT;
            break;

        case 11:
            header = DEP_AIRPORT;
            break;

        case 12:
            header = EOBT;
            break;

        case 13:
            header = RVSM;
            break;

        case 14:
            header = FR;
            break;

        case 15:
            header = ACTF_TURB.toUpper();
            break;

        case 16:
            header = TIME_STAMP.toUpper();
            break;
        }

        return header;
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

void Track_data_table_model_02::refresh_model()
{
    beginResetModel();
    endResetModel();
}

bool Track_data_table_model_02::has_flight_track() const
{
    bool flag(false);

    if ((nullptr != list_)
            && (0 < list_->count())) {
        flag = true;
    }

    return flag;
}

void Track_data_table_model_02::clear_track_data()
{
    if ((nullptr != list_)
            && (0 < list_->count())) {
//        delete  list_;
        list_->clear();
        list_ = new QList<Track_data_02*>();
    }
}

QList<Track_data_02 *> *Track_data_table_model_02::get_backup_track_data()
{
    return backup_list_;
}

void Track_data_table_model_02::set_current_page(int page)
{
    current_page_ = page;
}

int Track_data_table_model_02::get_current_page() const
{
    return current_page_;
}

void Track_data_table_model_02::set_data_size_in_page(int data_size)
{
    if (0 >= data_size) {
        return;
    }

    data_size_in_page_ = data_size;
    set_current_page(0);

    refresh_model();
}

int Track_data_table_model_02::get_data_size_in_page() const
{
    return data_size_in_page_;
}

int Track_data_table_model_02::get_page_count() const
{
    return (0 == list_count() % data_size_in_page_)
            ? (list_count() / data_size_in_page_)
            : ((list_count() / data_size_in_page_) + 1);
}

void Track_data_table_model_02::sl_add_track_data(Track_data_02* data)
{
    list_->append(data);

    backup_list_->append(data);

    // Refresh View
    emit layoutChanged();
}

void Track_data_table_model_02::sl_add_filter_track_data(int row, bool flag)
{
    // Append track data to list_ for displaying in View
    if ( flag) {
        list_->append(backup_list_->at(row));
    }

    emit layoutChanged();
}

void Track_data_table_model_02::sl_update_model_by_filter()
{
    emit layoutChanged();
}

void Track_data_table_model_02::sl_clear_list()
{
    list_->clear();

    emit layoutChanged();
}

int Track_data_table_model_02::list_count() const
{
    if (nullptr == list_) {
        return 0;
    } else {
        return list_->size();
    }
}
